﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Trippy.Animation;
using Trippy.CollidableGameComponents.Agents;
using Trippy.CollidableGameComponents.Objects;
using Trippy.CollidableGameComponents.Events;
using Trippy.Levels;


namespace Trippy.CollidableGameComponents.Projectiles
{
    class Missile : CollidableGameComponent
    {
        public static Texture2D SpriteSheet { get; private set; }

        float TimeToLive = 5f;

        public Missile(Level level)
            : base(level)
        {
            if (SpriteSheet == null) SpriteSheet = CurrentLevel.Game.Content.Load<Texture2D>("Sprites/Projectiles/rocket");
            Sprite = new AnimatedSprite(this, SpriteSheet, 30, 62, 0, 0, 2, 0, 24, 0.3f);
            Friction = 0f;
        }

        public override void Update(GameTime gameTime)
        {
            if (Velocity.X >= 0)
                Sprite.ChangeAnimation(0);
            else
                Sprite.ChangeAnimation(1);
            Sprite.Step();
            UpdateStandardPhysics(TrippyUtilities.CalculateElapsedTime(gameTime));

            TimeToLive -= TrippyUtilities.CalculateElapsedTime(gameTime);
            if (TimeToLive <= 0)
            {
                new EffectAnimation(CurrentLevel, EffectAnimation.Effects.Explosion, Position);
                MarkForDeletion();
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            StandardDraw(gameTime);
            base.Draw(gameTime);
        }

        protected override void CollisionHandler(CollidableGameComponent component, CollidableGameComponent.CollisionSide side)
        {
            if (!(component is Orb) && !(component is PowerUp) && !(component is Event) && !(component is Hero))
            {
                new EffectAnimation(CurrentLevel, EffectAnimation.Effects.Explosion, Position);
                MarkForDeletion();
            }
            if (component is Enemy || component is PathingEnemy || component is SwoopingEnemy)
            {
                if (component.Sprite.Tint != Color.Red) //if enemy is not already dying
                {
                    component.Sprite.Tint = Color.Red;
                    component.MarkForDeletion(0.2f);
                }
            }
            if (component is SlaveOrb)
            {
                var slave = (SlaveOrb)component;


                MarkForDeletion();

                slave.DoDamage(1);

            }
            if (component is Ian)
            {
                var slave = (Ian)component;


                MarkForDeletion();

                slave.DoDamage(1);
            }

            base.CollisionHandler(component, side);
        }

    }
}
